<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Grafana-HTTPAPI(1)(Authentication篇+DataSource篇) | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="监控平台">
    <meta name="description" content="准备工作准备httpClient工具类，需要用到依赖httpclient 4.3.5,jackson-mapper-asl 1.9.13,slf4j-log4j12 1.7.7：123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960">
<meta name="keywords" content="监控平台">
<meta property="og:type" content="article">
<meta property="og:title" content="Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="准备工作准备httpClient工具类，需要用到依赖httpclient 4.3.5,jackson-mapper-asl 1.9.13,slf4j-log4j12 1.7.7：123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960">
<meta property="og:updated_time" content="2017-12-05T09:56:09.142Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)">
<meta name="twitter:description" content="准备工作准备httpClient工具类，需要用到依赖httpclient 4.3.5,jackson-mapper-asl 1.9.13,slf4j-log4j12 1.7.7：123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-11-19T16:00:00.000Z" itemprop="datePublished" class="page-time">
  2017-11-20
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#准备工作"><span class="post-toc-number">1.</span> <span class="post-toc-text">准备工作</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Authentication-API"><span class="post-toc-number">2.</span> <span class="post-toc-text">Authentication API</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Create-API-Token"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">Create API Token</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Auth-HTTP-resources-actions"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">Auth HTTP resources / actions</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Create-API-Key"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">Create API Key</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Delete-API-Key"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">Delete API Key</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Data-source-API"><span class="post-toc-number">3.</span> <span class="post-toc-text">Data source API</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Get-all-datasources"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">Get all datasources</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Get-a-single-data-sources-by-Id-Name"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">Get a single data sources by (Id/Name)</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Create-data-source"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">Create data source</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Update-an-existing-data-source"><span class="post-toc-number">3.4.</span> <span class="post-toc-text">Update an existing data source</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Delete-an-existing-data-source-by-id-Name"><span class="post-toc-number">3.5.</span> <span class="post-toc-text">Delete an existing data source by (id/Name)</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-grafana_http_api_1"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年11月20日 0:00" datetime="2017-11-19T16:00:00.000Z"  itemprop="datePublished">2017-11-20</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><p>准备httpClient工具类，需要用到依赖httpclient 4.3.5,jackson-mapper-asl 1.9.13,slf4j-log4j12 1.7.7：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 你需要准备doGet doPost doPut doDelete方法以便操作HTTP API，这里展示doGet doPost方法：</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(String uri, String token)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    CloseableHttpClient httpclient = HttpClients.createDefault();</div><div class="line">    HttpGet httpGet = <span class="keyword">new</span> HttpGet(uri);</div><div class="line">    httpGet.setHeader(<span class="string">"Authorization"</span>, <span class="string">"Bearer "</span> + token);</div><div class="line">    httpGet.setHeader(<span class="string">"Content-Type"</span>, <span class="string">"application/json"</span>);</div><div class="line">    httpGet.setHeader(<span class="string">"Accept"</span>, <span class="string">"application/json"</span>);</div><div class="line">    CloseableHttpResponse response = <span class="keyword">null</span>;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">        response = httpclient.execute(httpGet);</div><div class="line">        <span class="keyword">if</span> (response.getStatusLine().getStatusCode() == <span class="number">200</span>) &#123;</div><div class="line">            String content = EntityUtils.toString(response.getEntity(), <span class="string">"UTF-8"</span>);</div><div class="line">            System.out.println(content);</div><div class="line">        &#125;</div><div class="line">    &#125; <span class="keyword">finally</span> &#123;</div><div class="line">        <span class="keyword">if</span> (response != <span class="keyword">null</span>) &#123;</div><div class="line">            response.close();</div><div class="line">        &#125;</div><div class="line">        httpclient.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(String uri, String token, Object json)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    CloseableHttpClient httpclient = HttpClients.createDefault();</div><div class="line">    HttpPost httpPost = <span class="keyword">new</span> HttpPost(<span class="keyword">new</span> URIBuilder(uri).build());</div><div class="line">    httpPost.setHeader(<span class="string">"Authorization"</span>, <span class="string">"Bearer "</span> + token);</div><div class="line">    httpPost.setHeader(<span class="string">"Content-Type"</span>, <span class="string">"application/json"</span>);</div><div class="line">    httpPost.setHeader(<span class="string">"Accept"</span>, <span class="string">"application/json"</span>);</div><div class="line">    CloseableHttpResponse response = <span class="keyword">null</span>;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">        ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</div><div class="line">        String jsonString = mapper.writeValueAsString(json);</div><div class="line">        StringEntity entity = <span class="keyword">new</span> StringEntity(jsonString);</div><div class="line">        entity.setContentEncoding(<span class="string">"UTF-8"</span>);</div><div class="line">        entity.setContentType(<span class="string">"application/json"</span>);<span class="comment">// 发送json数据需要设置contentType</span></div><div class="line">        httpPost.setEntity(entity);</div><div class="line">        response = httpclient.execute(httpPost);</div><div class="line">        <span class="keyword">if</span> (response.getStatusLine().getStatusCode() == <span class="number">200</span>) &#123;</div><div class="line">            String content = EntityUtils.toString(response.getEntity(), <span class="string">"UTF-8"</span>);</div><div class="line">            System.out.println(content);</div><div class="line">        &#125;</div><div class="line">    &#125; <span class="keyword">finally</span> &#123;</div><div class="line">        <span class="keyword">if</span> (response != <span class="keyword">null</span>) &#123;</div><div class="line">            response.close();</div><div class="line">        &#125;</div><div class="line">        httpclient.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">doDelete</span><span class="params">(String uri, String token)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    CloseableHttpClient httpclient = HttpClients.createDefault();</div><div class="line">    HttpDelete httpDelete = <span class="keyword">new</span> HttpDelete(uri);</div><div class="line">    httpDelete.setHeader(<span class="string">"Authorization"</span>, <span class="string">"Bearer "</span> + token);</div><div class="line">    httpDelete.setHeader(<span class="string">"Content-Type"</span>, <span class="string">"application/json"</span>);</div><div class="line">    httpDelete.setHeader(<span class="string">"Accept"</span>, <span class="string">"application/json"</span>);</div><div class="line">    CloseableHttpResponse response = <span class="keyword">null</span>;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">        response = httpclient.execute(httpDelete);</div><div class="line">        <span class="keyword">if</span> (response.getStatusLine().getStatusCode() == <span class="number">200</span>) &#123;</div><div class="line">            String content = EntityUtils.toString(response.getEntity(), <span class="string">"UTF-8"</span>);</div><div class="line">            System.out.println(content);</div><div class="line">        &#125;</div><div class="line">    &#125; <span class="keyword">finally</span> &#123;</div><div class="line">        <span class="keyword">if</span> (response != <span class="keyword">null</span>) &#123;</div><div class="line">            response.close();</div><div class="line">        &#125;</div><div class="line">        httpclient.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="Authentication-API"><a href="#Authentication-API" class="headerlink" title="Authentication API"></a>Authentication API</h2><h3 id="Create-API-Token"><a href="#Create-API-Token" class="headerlink" title="Create API Token"></a>Create API Token</h3><p>如果启用了基本身份验证（默认启用），则可以通过标准的基本身份验证来验证您的HTTP请求。如果使用面板，可以在侧边菜单栏中选择API Keys添加授权(<strong>注意：添加用户后记得保存好token</strong>)。</p>
<h3 id="Auth-HTTP-resources-actions"><a href="#Auth-HTTP-resources-actions" class="headerlink" title="Auth HTTP resources / actions"></a>Auth HTTP resources / actions</h3><p><strong>GET /api/auth/keys</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">HttpClientUtil.doGet(<span class="string">"http://119.29.141.219:3000/api/auth/keys"</span>, token);</div><div class="line"><span class="comment">/* output:</span></div><div class="line"><span class="comment">[&#123;"id":6,"name":"tom","role":"Admin"&#125;,&#123;"id":1,"name":"TestAdmin","role":"Admin"&#125;]</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></p>
<h3 id="Create-API-Key"><a href="#Create-API-Key" class="headerlink" title="Create API Key"></a>Create API Key</h3><p><strong>POST /api/auth/keys</strong></p>
<ul>
<li>name： 密钥名字</li>
<li>role – 设置密钥的访问级别/ Grafana角色。可以是下列值之一：Viewer，Editor，Read Only Editor或Admin。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> String name;</div><div class="line">    <span class="keyword">private</span> String role;</div><div class="line">    <span class="comment">//...</span></div><div class="line"></div><div class="line">User user = <span class="keyword">new</span> User(<span class="string">"sqdyy"</span>, <span class="string">"Admin"</span>);</div><div class="line">HttpClientUtil.doPost(<span class="string">"http://119.29.141.219:3000/api/auth/keys"</span>, token ,user);</div><div class="line"><span class="comment">/* output:</span></div><div class="line"><span class="comment">&#123;"name":"sqdyy","key":"eyJrIjoiVHpRYUVueVh4S09EclJKWmhMbmw0MGJaVklkMnA0QUYiLCJuIjoic3FkeXkiLCJpZCI6MX0="&#125;</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure>
<h3 id="Delete-API-Key"><a href="#Delete-API-Key" class="headerlink" title="Delete API Key"></a>Delete API Key</h3><p><strong>DELETE /api/auth/keys/:id</strong>，这里只能通过id删除，所以删除前必须先<strong>GET /api/auth/keys</strong>得到用户id<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">HttpClientUtil.doDelete(<span class="string">"http://119.29.141.219:3000/api/auth/keys/8"</span>, token);</div><div class="line"><span class="comment">/* output:</span></div><div class="line"><span class="comment">&#123;"message":"API key deleted"&#125;</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></p>
<hr>
<h2 id="Data-source-API"><a href="#Data-source-API" class="headerlink" title="Data source API"></a>Data source API</h2><h3 id="Get-all-datasources"><a href="#Get-all-datasources" class="headerlink" title="Get all datasources"></a>Get all datasources</h3><p><strong>GET /api/datasources</strong><br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">[</div><div class="line">    &#123;<span class="attr">"id"</span>:<span class="number">3</span>,</div><div class="line">    <span class="attr">"orgId"</span>:<span class="number">1</span>,</div><div class="line">    <span class="attr">"name"</span>:<span class="string">"javamelody"</span>,</div><div class="line">    <span class="attr">"type"</span>:<span class="string">"influxdb"</span>,</div><div class="line">    <span class="attr">"typeLogoUrl"</span>:<span class="string">"public/app/plugins/datasource/influxdb/img/influxdb_logo.svg"</span>,</div><div class="line">    <span class="attr">"access"</span>:<span class="string">"proxy"</span>,</div><div class="line">    <span class="attr">"url"</span>:<span class="string">"http://119.29.141.219:8086"</span>,</div><div class="line">    <span class="attr">"password"</span>:<span class="string">"密码"</span>,</div><div class="line">    <span class="attr">"user"</span>:<span class="string">"sqdyy"</span>,</div><div class="line">    <span class="attr">"database"</span>:<span class="string">"javamelody"</span>,</div><div class="line">    <span class="attr">"basicAuth"</span>:<span class="literal">false</span>,</div><div class="line">    <span class="attr">"isDefault"</span>:<span class="literal">false</span>,</div><div class="line">    <span class="attr">"jsonData"</span>:&#123;&#125;</div><div class="line">    &#125;,</div><div class="line">    &#123;<span class="attr">"id"</span>:<span class="number">2</span>,</div><div class="line">    <span class="attr">"orgId"</span>:<span class="number">1</span>,</div><div class="line">    <span class="attr">"name"</span>:<span class="string">"telegraf"</span>,</div><div class="line">    <span class="attr">"type"</span>:<span class="string">"influxdb"</span>,</div><div class="line">    "typeLogoUrl":"public/app/plugins/datasource/influxdb/img/influxdb_logo.svg", // 这个参数是固定的改不的</div><div class="line">    "access":"proxy",</div><div class="line">    "url":"http://119.29.141.219:8086/",</div><div class="line">    "password":"密码",</div><div class="line">    "user":"telegraf",</div><div class="line">    "database":"telegraf",</div><div class="line">    "basicAuth":false, </div><div class="line">    "isDefault":true, </div><div class="line">    "jsonData":&#123;&#125; // CloudWatch才需要配这个</div><div class="line">    &#125;</div><div class="line">]</div></pre></td></tr></table></figure></p>
<h3 id="Get-a-single-data-sources-by-Id-Name"><a href="#Get-a-single-data-sources-by-Id-Name" class="headerlink" title="Get a single data sources by (Id/Name)"></a>Get a single data sources by (Id/Name)</h3><p><strong>GET /api/datasources/:datasourceId</strong> or <strong>GET /api/datasources/name/:name</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//HttpClientUtil.doGet("http://119.29.141.219:3000/api/datasources/2", token);</span></div><div class="line">HttpClientUtil.doGet(<span class="string">"http://119.29.141.219:3000/api/datasources/name/telegraf"</span>, token);</div></pre></td></tr></table></figure></p>
<h3 id="Create-data-source"><a href="#Create-data-source" class="headerlink" title="Create data source"></a>Create data source</h3><p><strong>POST /api/datasources</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/* PO类 */</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DBSource</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;<span class="comment">// 更新时需要指定</span></div><div class="line">    <span class="keyword">private</span> String name;<span class="comment">// 数据源名称</span></div><div class="line">    <span class="keyword">private</span> String type;<span class="comment">// 数据库类型</span></div><div class="line">    <span class="keyword">private</span> String url; <span class="comment">// 服务器地址+端口号</span></div><div class="line">    <span class="keyword">private</span> String password; <span class="comment">// 密码</span></div><div class="line">    <span class="keyword">private</span> String user; <span class="comment">// 用户名</span></div><div class="line">    <span class="keyword">private</span> String database; <span class="comment">// 数据库名称</span></div><div class="line">    <span class="comment">// 这个参数是必填的，有两个值:</span></div><div class="line">    <span class="comment">// (direct:URL is used directly from browser)</span></div><div class="line">    <span class="comment">// (proxy:Grafana backend will proxy the request)</span></div><div class="line">    <span class="keyword">private</span> String access;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> isDefault; <span class="comment">// 是否默认数据源</span></div><div class="line"></div><div class="line"><span class="comment">/* 测试 */</span></div><div class="line">DBSource dbSource = <span class="keyword">new</span> DBSource()</div><div class="line">        .setType(<span class="string">"influxdb"</span>)</div><div class="line">        .setUser(<span class="string">"sqdyy"</span>)</div><div class="line">        .setPassword(<span class="string">"密码"</span>)</div><div class="line">        .setUrl(<span class="string">"http://119.29.141.219:8086"</span>)</div><div class="line">        .setName(<span class="string">"HttpApiTest"</span>) <span class="comment">// 面板里的数据源名称</span></div><div class="line">        .setDatabase(<span class="string">"mydb"</span>)</div><div class="line">        .setAccess(<span class="string">"proxy"</span>)</div><div class="line">        .setDefault(<span class="keyword">false</span>);</div><div class="line">HttpClientUtil.doPost(<span class="string">"http://119.29.141.219:3000/api/datasources"</span>,token,dbSource);</div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">&#123;"id":6,"message":"Datasource added","name":"HttpApiTest"&#125;</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></p>
<h3 id="Update-an-existing-data-source"><a href="#Update-an-existing-data-source" class="headerlink" title="Update an existing data source"></a>Update an existing data source</h3><p><strong>PUT /api/datasources/:datasourceId</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">DBSource dbSource = <span class="keyword">new</span> DBSource()</div><div class="line">        .setId(<span class="number">6</span>)</div><div class="line">        .setUser(<span class="string">"sqdyy"</span>)</div><div class="line">        .setUrl(<span class="string">"http://119.29.141.219:8086"</span>)</div><div class="line">        .setPassword(<span class="string">"123456"</span>)</div><div class="line">        .setName(<span class="string">"HttpApiTestUpdate"</span>)</div><div class="line">        .setType(<span class="string">"influxdb"</span>)</div><div class="line">        .setDatabase(<span class="string">"mydb"</span>)</div><div class="line">        .setAccess(<span class="string">"proxy"</span>)</div><div class="line">        .setDefault(<span class="keyword">false</span>);</div><div class="line">HttpClientUtil.doPut(<span class="string">"http://119.29.141.219:3000/api/datasources/6"</span>,token,dbSource);</div><div class="line"><span class="comment">/* output:</span></div><div class="line"><span class="comment">&#123;"id":6,"message":"Datasource updated","name":"HttpApiTestUpdate"&#125;</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></p>
<h3 id="Delete-an-existing-data-source-by-id-Name"><a href="#Delete-an-existing-data-source-by-id-Name" class="headerlink" title="Delete an existing data source by (id/Name)"></a>Delete an existing data source by (id/Name)</h3><p><strong>DELETE /api/datasources/:datasourceId</strong> or <strong>DELETE /api/datasources/name/:datasourceName</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// HttpClientUtil.doPost("http://119.29.141.219:3000/api/datasources",token,dbSource);*/</span></div><div class="line">HttpClientUtil.doDelete(<span class="string">"http://119.29.141.219:3000/api/datasources/name/HttpApiTestUpdate"</span>, token);</div><div class="line"><span class="comment">/* output:</span></div><div class="line"><span class="comment">&#123;"message":"Data source deleted"&#125;</span></div><div class="line"><span class="comment">*/</span></div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:56:09.142Z" itemprop="dateUpdated">2017年12月5日 17:56</time>
</span><br>


        如要转载请注明出处：<a href="/2017/11/20/grafana_http_api_1/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/监控平台/">监控平台</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&title=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&title=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/11/27/tomcat_managerServlet/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">tomcat ManagerServlet阅读源码后的实践</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/11/13/Metrics/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Metrics-JVM上的实时监控类库</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&title=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&title=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/11/20/grafana_http_api_1/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
